class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if(s1+s2 == s3)
            return true;
        int m = s1.size();
        int n = s2.size();
        if(m+n != s3.size())
            return false;
        vector<vector<int>> dp(m+1, vector<int>(n+1));

        dp[0][0] = 0;
        int goon = 1;
        for(int i = 0; i < m; i++)
        {
            if(s1[i] != s3[i])
                goon = 0;
            if(goon)
                dp[i+1][0] = 1;
            else
                dp[i+1][0] = 0;
        }

        goon = 1;
        for(int j = 0; j < n; j++)
        {
            if(s2[j] != s3[j])
                goon = 0;
            if(goon)
                dp[0][j+1] = 1;
            else
                dp[0][j+1] = 0;
        }

        for(int i = 1; i <= m; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(dp[i-1][j] && s1[i-1]==s3[i+j-1])
                    dp[i][j] = 1;
                else if(dp[i][j-1] && s2[j-1]==s3[i+j-1])
                    dp[i][j] = 1;
                else
                    dp[i][j] = 0;
            }
        }

        return dp[m][n];
    }
};